package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tan.model.domain.Question;
import com.tan.model.domain.Settings;
import com.tan.model.domain.UserInfo;
import com.tan.model.vo.PageResult;
import com.tan.model.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.Userthread;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingService {
    @DubboReference
    SettingsApi settingsApi;
    @DubboReference
    QuestionApi questionApi;
    @DubboReference
    BlackListApi blackListApi;
    //查询通用设置
    public SettingsVo getSettings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = Userthread.getUser().getId();
        settingsVo.setId(userId);
       String mobile = Userthread.getUser().getMobile();
        settingsVo.setPhone(mobile);
      Question question = questionApi.findByUserId(userId);
      String txt = question.getTxt();
      if (txt == null) {
          txt = "Java";
      }
      settingsVo.setStrangerQuestion(txt);
     Settings settings = settingsApi.findByUserId(userId);
     if (settings != null) {
         settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
         settingsVo.setLikeNotification(settings.getLikeNotification());
         settingsVo.setPinglunNotification(settings.getPinglunNotification());
     }
        return settingsVo;
    }

    public void updateQuestion(String content) {
        Long userId = Userthread.getUser().getId();
        Question question = questionApi.findByUserId(userId);
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else{
            question.setTxt(content);
            questionApi.updatetxt(question);
        }

    }

//    public void saveSettings(Map map) {
//            String likeNotification=(String) map.get("likeNotification");
//      String pinglunNotification=(String) map.get("pinglunNotification");
//   String gonggaoNotification=(String) map.get("gonggaoNotification");
//    }

    public void setSettings(Map map) {
        Long userId = Userthread.getUser().getId();
        Settings settings = settingsApi.findByUserId(userId);
        if (settings==null){
            settings = new Settings();
            Boolean likeNotification=(Boolean) map.get("likeNotification");
            Boolean pinglunNotification= (Boolean) map.get("pinglunNotification");
            Boolean gonggaoNotification= (Boolean) map.get("gonggaoNotification");
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        }else {
            Boolean likeNotification=(Boolean) map.get("likeNotification");
            Boolean pinglunNotification= (Boolean) map.get("pinglunNotification");
            Boolean gonggaoNotification= (Boolean) map.get("gonggaoNotification");
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
    }

    public PageResult getBlackList(int page, int pagesize) {
       Long userId= Userthread.getUser().getId();
      //调用api调查用户黑名单分页列表，Ipage对象
        IPage<UserInfo> ipage = blackListApi.getBlackList(userId, page, pagesize);
     int count = (int) ipage.getTotal();
        PageResult pageResult = new PageResult(page,pagesize,count,ipage.getRecords());
        return pageResult;
    }

    public void deleteBlackList(Long blackUserId) {
    Long userId=    Userthread.getUser().getId();
        blackListApi.deleteBlackList(blackUserId,userId);
    }
}
